;;; init-minibuffer.el --- Mini-buffer settings -*- lexical-binding: t -*-
;;; Commentary:

;;; Code:

;; 以任意的顺序匹配正则表达式
(use-package orderless
  :defer 2
  :ensure t
  :custom
  (orderless-component-separator #'orderless-escapable-split-on-space) ;使用反斜杠来转义空格，转义空格将被视为字符而不是分隔符
  (completion-styles '(orderless basic))
  (completion-category-defaults nil)
  (completion-category-overrides '((file (styles partial-completion)))))

;; 为 minibuffer 输入的命令提供候选面板
(use-package vertico
  :ensure t
  :hook (elpaca-after-init . vertico-mode)
  :custom
  (vertico-scroll-margin 0)
  (vertico-cycle t)                     ; Optionally enable cycling for `vertico-next' and `vertico-previous'.
  :bind (:map vertico-map
              ("/"     . #'+vertico/vertico-insert)
              ("M-RET" . #'vertico-exit-input)))

;; vertico 在文件夹补全方面的拓展
(use-package vertico-directory
  :after vertico
  :ensure nil
  ;; More convenient directory navigation commands
  :bind (:map vertico-map
              ("RET"   . #'vertico-directory-enter)
              ("DEL"   . #'delete-backward-char)
              ("M-DEL" . #'vertico-directory-delete-word))
  ;; Tidy shadowed file names
  :hook (rfn-eshadow-update-overlay . vertico-directory-tidy))

;; 基于 vertico 的文本搜索
(use-package consult
  :after vertico
  :ensure t
  :custom
  (consult-preview-key 'any)            ; consult 预览按键
  (consult-preview-excluded-files
   '("\\`/[^/|:]+:" ".*"))              ; 所有文件都不被预览
  (consult-preview-excluded-buffers
   (lambda (_) t))                      ; 所有 buffer 都不被预览
  (consult-narrow-key "<")              ; 缩小搜索范围，比如 `consult-buffer' 后，按下 `<' 则会提示缩小搜索范围
  (consult-widen-key ">")               ; 扩展搜索范围
  (consult-async-min-input 1))          ; 开始异步搜索的最小输入长度

(use-package consult-projectile
  :ensure t
  :commands (consult-projectile-find-dir
             consult-projectile-find-file
             consult-projectile-switch-project))

;; 根据光标所在的位置来提供所需的命令
(use-package embark
  :ensure t
  :init
  ;; Optionally replace the key help with a completing-read interface
  (setq prefix-help-command #'embark-prefix-help-command)
  :custom
  (embark-quit-after-action t)
  (prefix-help-command #'embark-prefix-help-command)
  (embark-cycle-key ".")
  (embark-help-key "?") ; C-x ? 可以调出所有以C-x开头的命令
  :config
  ;; Hide the mode line of the Embark live/completions buffers
  (add-to-list 'display-buffer-alist
               '("\\`\\*Embark Collect \\(Live\\|Completions\\)\\*"
                 nil
                 (window-parameters (mode-line-format . none)))))

(use-package embark-consult :after (embark consult) :ensure t)

;; 为 minibuffer 中的选项添加注解
(use-package marginalia
  :ensure t
  :hook (elpaca-after-init . marginalia-mode)
  :bind (:map minibuffer-local-map
              ("M-A" . #'marginalia-cycle)) ; 切换注释形式
  :config
  (advice-add #'marginalia-cycle :after
              (lambda ()
                (let ((inhibit-message t))
                  (customize-save-variable 'marginalia-annotator-registry
                                           marginalia-annotator-registry))))) ; 保存切换的模式

(provide 'init-minibuffer)
;;; init-minibuffer.el ends here
